import { Data } from "../../../../modeal/data.js"
const data = new Data()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hotWord: [],
    tagArr: [], //选中的热词
    inputText: "",
  },


  onLoad: function (options) { },
  onReady: function () { },
  onShow: function () {
    this.getHotWord();   //获取备注热词
  },

  // 获取备注热词
  getHotWord () {
    data.getHotWord().then(res => {
      let hotWord = []
      res.forEach((item, index) => {
        let temp = {}
        temp.checked = false
        temp.title = item
        hotWord.push(temp)
      });

      this.setData({ hotWord })
    })
  },

  // 点击单个热词
  hotCkick (e) {
    let hotWord = this.data.hotWord
    let index = e.currentTarget.dataset.index
    hotWord.forEach((item, i) => {
      if (index == i) {
        if (hotWord[i].checked == true) {
          hotWord[i].checked = false;
        }
        else {
          hotWord[i].checked = true;
        }
      }
    });
    let tagArr = hotWord.filter(item => {
      return item.checked
    })
    this.setData({ hotWord, tagArr })
  },

  inputText (e) {
    let inputText = e.detail.value
    this.setData({ inputText })
  },

  submit () {
    // 返回上个页面
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    let tagArr = this.data.tagArr
    let inputText = this.data.inputText
    let temp = []
    tagArr.forEach(item => {
      temp.push(item.title)
    });
    temp.push(inputText)

    // 给上个页面赋值 并返回上一页
    prevPage.setData({ remarkText: temp })
    wx.navigateBack({ delta: 1 })
  }

})